package com.beifu.manager.api.controller.feedback;

import com.beifu.manager.api.common.PageInfo;
import com.beifu.manager.api.domain.feedback.FeedBackDetailResponse;
import com.beifu.manager.api.domain.feedback.FeedBackInfo;
import com.beifu.manager.api.domain.feedback.FeedBackParam;
import com.beifu.manager.api.domain.warehouse.InWarehouseResponse;
import com.beifu.manager.model.WashFeedback;
import com.beifu.manager.service.washfeedback.WashFeedBackService;
import com.utils.JsonUtil;
import com.utils.ResponseHelp;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 再壮 on 2018/6/12.
 */
@RestController
@RequestMapping(value="/mobileserve/feedBack/api", produces = "application/json;charset=UTF-8")
public class FeedBackController {
	@Autowired
	private WashFeedBackService washFeedBackService;

    /**
     * 洗涤反馈列表
     * @param reqMap
     * @return
     */
    @RequestMapping(value="/getFeedBackList", method= RequestMethod.POST)
    public String getInWarehouseList(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
			userId = (String) jsonPub.get("userId");
			devID = (String) jsonPub.get("devID");
			timestamp = (String) jsonPub.get("timestamp");
		}
        
        if(JSONUtils.isNull(jsonIn)){
        	return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }

        if (null == jsonIn.get("pageIndex") || StringUtils.isBlank(jsonIn.get("pageIndex").toString())) {
            return ResponseHelp.responseApiErrorText("1111", "pageIndex不能为空！");
        }
        if (null == jsonIn.get("pageNumber") || StringUtils.isBlank(jsonIn.get("pageNumber").toString())) {
            return ResponseHelp.responseApiErrorText("1112", "pageNumber不能为空！");
        }

        String pageIndex = jsonIn.get("pageIndex").toString();
        String pageNumber = jsonIn.get("pageNumber").toString();


        List<FeedBackInfo> list = washFeedBackService.getFeedBackList(pageIndex, pageNumber,userId);

        PageInfo pageInfo = new PageInfo();

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("assetsOutList", list);

        pageInfo.setResult(result);
        pageInfo.setPageIndex(Integer.valueOf(pageIndex));
        pageInfo.setPageNum(Integer.valueOf(pageNumber));

        return ResponseHelp.responseApiSuccessText(pageInfo);
    }


    /**
     * 反馈单详情
     */
    @RequestMapping(value="/getFeedBackDetail")
    public String getFeedBackDetail(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));
        // 获取具体值，先这样，
        // TODO 待优化
        String userId = jsonPub.get("userId").toString();
        String devID = jsonPub.get("devID").toString();
        String timestamp = jsonPub.get("timestamp").toString();
        // 接口参数
        if(JSONUtils.isNull(jsonIn)){
        	return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }
        if (jsonIn.get("washBackId") == null) {
            return ResponseHelp.responseApiErrorText("1111", "反馈单ID为空！");
        }
        
        String id = (String) jsonIn.get("washBackId");
        FeedBackDetailResponse feedBackDetailResponse = washFeedBackService.getFeedBackDetail(id);
        
        return ResponseHelp.responseApiSuccessText(feedBackDetailResponse);
    }


    /**
     * 反馈详情上传
     * @throws ParseException 
     */
    @RequestMapping(value="/uploadFeedBackDetail")
    public String uploadFeedBackDetail(@RequestBody Map<String, Object> reqMap) throws ParseException {
        WashFeedback washFeedBack = new WashFeedback();




        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));





        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
			userId = (String) jsonPub.get("userId");
			devID = (String) jsonPub.get("devID");
			timestamp = (String) jsonPub.get("timestamp");
		}
        if(JSONUtils.isNull(jsonIn)){
        	return ResponseHelp.responseApiErrorText("2222", "传入参数不能为空！");
        }
//        String applyName = (String) jsonIn.get("applyName");//申请人
        String applyDate = (String) jsonIn.get("applyDate");//申请时间
        String desc = (String) jsonIn.get("desc");//反馈内容
        List<String> imgs = (List<String>) jsonIn.get("imgs");//反馈图片

        String trainTime = (String) jsonIn.get("trainTime");//执行车次

        JSONArray jsonArray = (JSONArray) jsonIn.get("scrapList");

        if(null!=jsonIn.get("washId")){
            String washId = (String)jsonIn.get("washId") ;//洗涤单编号
            washFeedBack.setAssetRfidCode(washId);
        }

        //将json数组装换为JavaBean
        List<FeedBackParam> scrapList = new ArrayList<FeedBackParam>();
        if(null != jsonArray && jsonArray.size() > 0){
	        for (Object jsonObject : jsonArray) {
	        	FeedBackParam feedBackParam = JsonUtil.fromJson(jsonObject.toString(), FeedBackParam.class);
	        	scrapList.add(feedBackParam);
			}
        }
        
        //将前端获取的参数封装

        washFeedBack.setCreateUserId(Integer.parseInt(userId));

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date createTime = dateFormat.parse(applyDate);
        washFeedBack.setCreateTime(createTime);

        washFeedBack.setTrainNumber(trainTime);
        washFeedBack.setImgList(imgs);
        washFeedBack.setFeedbackContent(desc);
        washFeedBack.setFeedbackStatus("1");
        washFeedBackService.saveWashFeedBack(washFeedBack, scrapList);
        
        return ResponseHelp.responseApiSuccessText(null);
    }
}
